/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shooter;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEmitter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.shooter.level.ScoreManager;
import java.security.SecureRandom;

/**
 *
 * @author Unni Vemanchey Mana
 */
public class WorldRenderer implements ITimer{

    private SpriteBatch batch;
    private World world;
    private final String PLAYERSPRITE = "player.png";
    private Texture jetTexture;
    private Texture playerTexture;
    private Texture backGroundTexture;
    private final String JETSPRITE = "jets.png";
    ParticleEffect effect;
    Array<ParticleEmitter> emitters;
    private Player player;
    private int count = 500;
    private Star[] stars = new Star[count];
    private SpaceBuddie spaceBuddie;
    private BitmapFont starFfont;
    private int width;
    private int height;
    private float ppuX;	// pixels per unit on the X axis
    private float ppuY;	// pixels per unit on the Y axis
    private static final float CAMERA_WIDTH = 10f;
    private static final float CAMERA_HEIGHT = 7f;
    private Bomber bomber;
    private ITimer timer;
    private boolean loadBomb;
    private SpriteExplode se;
    private boolean isExploded;
    private Explosion explosion;
    private ScoreManager scoreManager;
    private  BulletManager bullet;
    private ShapeRenderer shapeRenderer ;
    private OrthographicCamera camera;
    private BitmapFont textFont;
    private Sound fireSnd;
    private Spikey spikey;
    
    public WorldRenderer(World world) {
        this.world = world;
        initialize();
    }

    WorldRenderer(World world, boolean b) {
        this.world = world;
        createCamera();
        initialize();
        
    }

    private void initialize() {
        batch     = new SpriteBatch();
        starFfont = new BitmapFont();
        player    = world.getPlayer();
        initializeSound();
        initializeTimer();
        loadTextures();
        initializeEffect();
        InitializeStars();
        initializeBaddie();
        initializeBomber();
        initializeBullet();
        initializeSpriteExplosion();
        initializeShapeRenderer();
        initializeSpikey();
        textFont = new BitmapFont();
    }

    private void createCamera(){
        this.camera = new OrthographicCamera(10, 7);
        this.camera.position.set(5, 3.5f, 0);
        this.camera.update();
    }
    
   
    public void loadTextures() {
        Texture.setEnforcePotImages(false);
        playerTexture = new Texture(Gdx.files.internal(PLAYERSPRITE));
        jetTexture = new Texture(Gdx.files.internal(JETSPRITE));
        backGroundTexture = new Texture(Gdx.files.internal("Background4.png"));
    }

    private void initializeEffect() {
        effect = new ParticleEffect();
        effect.load(Gdx.files.internal("particle1.p"), Gdx.files.internal(""));
        effect.start();
        emitters = new Array(effect.getEmitters());
        effect.getEmitters().clear();
        effect.getEmitters().add(emitters.get(0));
    }

    private void InitializeStars() {
        for (int i = 0; i < count; i++) {
            Star s = new Star();
            int x = new SecureRandom().nextInt(480);
            int y = new SecureRandom().nextInt(320);
            Vector2 vec = new Vector2(x,y);
            s.setPosition(vec);
            stars[i] = s;
        }
    }

    private void initializeSound(){
        fireSnd = Gdx.audio.newSound(Gdx.files.internal("LazerRicochet.wav"));
        player.setFiringSound(fireSnd);
    }
    
    private void initializeBomber(){
      bomber = new Bomber(player);
    }
    
    private void initializeBaddie() {
      spaceBuddie = new SpaceBuddie();
    }

    private void initializeTimer(){
     timer = this;
     GameTimer.start(timer);
    }
    
    private void initializeShapeRenderer(){
     shapeRenderer = new ShapeRenderer();
    }
    
    private void initializeSpriteExplosion(){
       scoreManager = new ScoreManager();
       explosion    = new Explosion(scoreManager); 
       se           = new SpriteExplode("ship3.png");
    }
    
    private void initializeSpikey(){
        spikey = new Spikey();
        spikey.showSpikey(true);
    }
    
    private void loadBomber(){
      bomber.loadBomber((int)player.getPosition().x, (int)player.getPosition().y);
    }
    
    private void initializeBullet(){
      bullet = new BulletManager(); 
      player.setBulletManager(bullet);
    }
    
    private void drawSpaceBuddie() {
      spaceBuddie.drawSpaceBuddie(batch);
    }

    public void drawPlayer() {
      player = world.getPlayer();
      Sprite sprite = player.getSprite();
      sprite.setPosition( player.getPosition().x, player.getPosition().y);
      //batch.draw(playerTexture, player.getPosition().x, player.getPosition().y);
      sprite.draw(batch);
    }

    private void drawJet(float d) {
      effect.setPosition(player.getPosition().x + 8, player.getPosition().y - 5);
      effect.draw(batch, d);
    }

    public void render() {
        batch.begin();
        GameTimer.calculateDeltaTime(0);
        drawBackGround();
        drawStars();
        drawPlayer();
        drawJet(Gdx.graphics.getDeltaTime());
        drawSpaceBuddie();
        drawBomb();
        drawExplosion();
        drawBullet();
        drawSpikey();
        batch.end();
    }

    public void drawBackGround(){
        batch.draw(backGroundTexture, -50, -50);
    }

    public void drawStars() {
      for(int i =0; i<count; i++){
          Star s = stars[i];
          if(s.getPosition().y < 0)s.getPosition().y = 320;
          starFfont.draw(batch, ".", s.getPosition().x,s.getPosition().y);
          s.getPosition().y = s.getPosition().y - 1.0f;
      }
    }

     private void drawFilledCircle(int x, int y){
       Gdx.gl.glEnable(GL10.GL_BLEND);
       Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
       shapeRenderer.setProjectionMatrix(camera.combined);
       shapeRenderer.begin(ShapeType.FilledCircle);
       shapeRenderer.setColor(new Color(0, 1, 0, 0.5f));
      shapeRenderer.filledCircle(460, 45, 10);
      shapeRenderer.filledCircle(x, y, 30);
       shapeRenderer.end();
       Gdx.gl.glDisable(GL10.GL_BLEND);
    }
     
     private void drawTextFont(int x, int y,CharSequence str){
       batch.begin();
       textFont.draw(batch, str, x, y);
       batch.end();
    } 
    
   private void drawSpikey(){
     spikey.drawSpikey(batch, player.getPosition().x, player.getPosition().y);           
   }  
   
   public void setSize (int w, int h){
     this.width  = w;
     this.height = h;
     ppuX        = (float)width / CAMERA_WIDTH;
     ppuY        = (float)height / CAMERA_HEIGHT;
   }
   
    private void drawBomb(){
      bomber.drawBomber(batch); 
    }
    
    public void drawBullet(){
         bullet.drawBullet(batch, player, spaceBuddie);  

    }

    public void drawExplosion(){
       se.drawSpriteExplosion(batch);              
    }

    @Override
    public void execute(){
      loadBomb = true;
      loadBomber();
    }
}
